
/*!
 * 界面入口模块  
 */

layui.extend({
  setter: 'config' //配置文件
  , admin: 'lib/admin' //核心模块
  , view: 'lib/view' //核心模块
}).define(['setter', 'admin'], function (exports) {
  var setter = layui.setter
    , element = layui.element
    , admin = layui.admin
    , tabsPage = admin.tabsPage
    , view = layui.view

    , openTabsPage = function (url, text) {
      //遍历页签选项卡
      var matchTo
        , tabs = $('#LAY_app_tabsheader>li')
        , path = url.replace(/(^http(s*):)|(\?[\s\S]*$)/g, '');

      tabs.each(function (index) {
        var li = $(this)
          , layid = li.attr('lay-id');

        if (layid === url) {
          matchTo = true;
          tabsPage.index = index;
        }
      });
      text = text || '新标签页';

      //定位当前tabs
      var setThisTab = function () {
        element.tabChange(FILTER_TAB_TBAS, url);
        admin.tabsBodyChange(tabsPage.index, {
          url: url
          , text: text
        });
      };
      if (setter.pageTabs) {
        //如果未在选项卡中匹配到，则追加选项卡
        if (!matchTo) {
          //延迟修复 Firefox 空白问题
          setTimeout(function () {
            $(APP_BODY).append([
              '<div class="layadmin-tabsbody-item layui-show">'
              , '<iframe src="/src/views/' + url + '.html" frameborder="0" class="layadmin-iframe"></iframe>'
              , '</div>'
            ].join(''));
            setThisTab();
          }, 10);

          tabsPage.index = tabs.length;
          element.tabAdd(FILTER_TAB_TBAS, {
            title: '<span>' + text + '</span>'
            , id: url
            , attr: path
          });

        }
      } else {
        var iframe = admin.tabsBody(admin.tabsPage.index).find('.layadmin-iframe');
        iframe[0].contentWindow.location.href = url;
      }

      setThisTab();
    }

    //入口页面
    , entryPage = function (fn) {
      var container = view(setter.container)
      //将模块根路径设置为 controller 目录
      layui.config({
        base: setter.base + 'controller/'
      });
      var local = layui.data(setter.tableName);
      if (!local[setter.request.tokenName]) {
        return location.href = '/src/views/user/login.html'; //跳转到登入页
      }
      //渲染后台结构
      container.render('layout').done(function () {
        layui.element.render();
        element.tabChange(FILTER_TAB_TBAS, '/');
        if (admin.screen() < 2) {
          admin.sideFlexible();
        }
      });
    }

    , APP_BODY = '#LAY_app_body', FILTER_TAB_TBAS = 'layadmin-layout-tabs'
    , $ = layui.$, $win = $(window);

  //初始主体结构
  layui.link(
    setter.base + 'style/admin.css?v=' + (admin.v + '-1')
    , function () {
      entryPage()
    }
    , 'layuiAdmin'
  );

  //扩展 lib 目录下的其它模块
  layui.each(setter.extend, function (key, value) {
    var mods = {}
      , _isArray = setter.extend.constructor === Array;
    mods[_isArray ? value : key] = '{/}' + setter.base + 'lib/extend/' + value;
    layui.extend(mods);
  });

  //对外输出
  exports('index', {
    openTabsPage: openTabsPage
  });
});
